<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-05-02
 * Time: 16:58
 */

namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\logic\Logic;
use app\common\model\Category;
class Brand extends Admin
{
    public function index()
    {
        $logic = new Logic();
        $logic->setValue('modelClass','app\common\model\Brand');
        $logic->setValue('column','id,title,catid');
        $logic->setValue('with','cat');
        $params = paramForm();
        if (\think\Request::instance()->isAjax()) {
            
            $params['paginate'] = true;
            if (isset($params['title'])){
                $params['title'] = array('like','%'.$params['title'].'%');
                
            }
            if (isset($params['catname'])){
                $catIds = Category::where('title','like','%'.$params['catname'].'%')->field('id')->select();
               
                foreach ($catIds as $id){
                    $tempArr[] = $id->id;
                }
                $params['catid'] = array('in',$tempArr);
            }
            
            $lists = $logic->getList($params);
            success($lists);
        } else {
            return $this->fetch();
        }
        
    }
    
    public function edit($id){
        $logic = new Logic();
        $logic->setValue('modelClass','app\common\model\Brand');
        $logic->setValue('column','id,title,catid');
        $logic->setValue('with','cat');
        $params = paramForm();
        $brand = $logic->findModel($id);
        $this->assign('model',$brand);
        return $this->fetch();
    }
    
    public function update($id){
        $logic = new Logic();
        $logic->setValue('modelClass','app\common\model\Brand');
        $params = paramForm();
        $brand = $logic->findModel($id);
        
        $file = request()->file('pic');
        if($file){
            $params['thumb']=$this->uploadToAlyun($file);
        }
    
        $rt = $logic->edit($id,$params);
        if($rt){
            $this->success('修改成功','/brand/index');
        }else{
            $this->error('修改失败');
        }
    }
    
}